<?php
namespace heihei\rest;

use Yii;
use yii\data\Pagination;
use yii\rest\Controller AS BaseController;

/**
 * 基础控制器
 */
class Controller extends BaseController
{
    /**
     * @inheritdoc
     */
    protected function serializeData($data)
    {
        $data = Yii::createObject($this->serializer)->serialize($data);
        return is_array($data) && isset($data['code']) ? $data : $this->success($data);
    }

    /**
     * 失败输出
     */
    public function fail($message, $errcode = null){
        $result = ['code'=>"FAIL",'message'=>$message];
        if($errcode !== null){
            $result['errcode'] = $errcode;
        }
        return $result;
    }

    /**
     * 成功输出
     */
    public function success($data, $message = 'OK', $extends = null){
        $result = ['code'=>"SUCCESS",'message'=>$message];
        if(is_array($extends)){
            $result = array_merge($result, $extends);
        }
        if($data || is_array($data) || is_object($data)){
            $result['data'] = $data;
        }       
        return $result;
    }
}
